import java.util.LinkedList;


public class Wave extends EnergyAttack{

	private Point source;
	private float direction;
	private float damage;
	private float length;
	private float speed;
	private boolean continuing;
	private boolean life;
	private Sprite sprite;
	private int characterNumber;
	private int maxCount;
	private int count;
	private final int MAX_LENGTH = 400;
	/**
	 * creates a new attack wave
	 * @param source the starting point of the wave
	 * @param direction the direction the wave will advance in
	 * @param damage the amount of damage the wave does.
	 */
	public Wave(Sprite sprite,Point source, float direction,float damage, int characterNumber)
	{
		count = 0;
		maxCount = 1000000;
		life = true;
		this.radius = 10;
		this.speed = 40;
		this.source = source;
		this.direction = direction;
		this.damage = damage;
		this.length = 2;
		this.continuing = true;
		this.sprite = sprite;
		this.characterNumber = characterNumber;
		this.currentPosition = new Point(source.getX(),source.getY());
	}
	/**
	 * overwrites the collision detection for normal objects
	 */
	public boolean checkCollision(Collidable collidable)
	{
		float lowx = source.getX();
		float lowy = source.getY();
		float highx = (int)currentPosition.getX() + Math.abs((float)(Math.cos(direction)*length));
		float highy = (int)currentPosition.getY() + Math.abs((float)(Math.sin(direction)*length));
		boolean state1 = (collidable.getCurrentPosition().getX() >= lowx) && (collidable.getCurrentPosition().getX() <= highx);
		boolean state2 = (collidable.getCurrentPosition().getX() <= lowx) && (collidable.getCurrentPosition().getX() >= highx);
		boolean state3 = (collidable.getCurrentPosition().getY() >= lowy) && (collidable.getCurrentPosition().getY() <= highy);
		boolean state4 = (collidable.getCurrentPosition().getY() <= lowy) && (collidable.getCurrentPosition().getY() >= highy);
	//	if(state1 || state2)
	//	{
		System.out.println(state3);
			if(state3 || state4)
			{
				System.out.println("Collided");
				return true;
			}
//		}
		return false;
	}
	
	public boolean isAlive()
	{
		return life;
	}

	public int getCurrentImage()
	{
		return sprite.getImg();
	}
	public float getAngle()
	{
		return direction;
	}
	public int getType()
	{
		return sprite.getType();
	}
	/**
	 * on a collision, the wave should "move" it's end to where the collision took place by changing its length accordingly
	 */
	public void onCollision()
	{
		
		if(length <= 0)
		{
			length = 0;
			life = false;
		}
	}
	/**
	 * finds the distance between 2 points
	 * @param p1 first point
	 * @param p2 second point
	 */
	private float distance(Point p1,Point p2)
	{
		float x = p1.getX()-p2.getX();
		float y = p1.getY()-p2.getY();
		return (float)Math.sqrt(x*x + y*y);
	}
	/**
	 * sets the speed of the wave
	 * @param speed
	 */
	public void setSpeed(float speed)
	{
		this.speed = speed;
	}
	public void setRadius(float radius)
	{
		this.radius = radius;
	}
	/**
	 * tells the wave that it is still being produced
	 */
	public void add()
	{
		if(continuing == true)
		{
			length = length + speed;
			if(length >= MAX_LENGTH)
			{
				length = MAX_LENGTH;
			}
			else
			{
				currentPosition.setX(currentPosition.getX() + (float)(speed * Math.cos(direction)));
				currentPosition.setY(currentPosition.getY() + (float)(speed * Math.sin(direction)));
			}
			count = (int)length;

		}
		else
		{
			count = (int) (count + speed);
			if(count>MAX_LENGTH)
			{
				length = length - speed;
			}
			if(length <= 20)
			{
				life = false;
			}
			source.setX(source.getX() + (float)(speed*Math.cos(direction)));
			source.setY(source.getY() + (float)(speed*Math.sin(direction)));
		}
	}
	/**
	 * gets a copy of the source location of the wave
	 * @return copy of source
	 */
	public Point getSource()
	{
		return new Point(source.getX(),source.getY());
	}
	/**
	 * tells the wave to stop lengthening itself and just to move forward
	 */
	public void done()
	{
		continuing = false;
		maxCount = (int)length + 200;
	}
	
	public String getDrawableString()
	{
		String ans = "";
		int x = (int)(source.getX());
		String temp0 =  x +"";
		temp0 = paddZeros(temp0,7);
		int y = (int)(source.getY());
		String temp1 = y +"";
		temp1 = paddZeros(temp1,7);
		String temp2 = ((int)Math.toDegrees((getAngle()))) + "";
		temp2 = paddZeros(temp2,7);
		String temp3 = characterNumber + "";
		temp3 = paddZeros(temp3,2);
		String temp4 = sprite.getType() + "";
		temp4 = paddZeros(temp4,2);
		String temp5 = sprite.getImg() + "";
		temp5 = paddZeros(temp5,1);
		ans = "Wave: " + temp0 + temp1 + temp2 + temp3 + temp4 + temp5 + paddZeros(((int)length) + "",4) + "\n";
		return ans;
	}
	public String paddZeros(String str,int gg)
	{
		String a = str + "";
		if(a.length() > gg)
		{
			str = str.substring(0,gg);
		}
		while(a.length() < gg)
		{
			if(str.charAt(0) == '-')
			{
				String temp = "-0" + a.substring(1);
				a = temp;
			}
			else
			{
				a = "0" + a;
			}
		}
		return a;
	}
}
